package org.grow.commodity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.basic.dto.BatchIntentionDto;
import org.grow.commodity.dao.StorageDao;
import org.grow.commodity.entity.PagedResults;
import org.grow.commodity.entity.Storage;
import org.grow.commodity.entity.StorageQueryCondition;
import org.grow.commodity.entity.StorageSearchForm;
import org.grow.commodity.exception.CommodityException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 21-8-10
 */

@RestController
@RequestMapping("/commodity/storage")
public class StorageController {

    @Autowired
    private StorageDao storageDao;

    @PostMapping("/query")
    public PagedResults<Storage> query(@RequestBody StorageSearchForm storageSearchForm) {
        Integer pageNum = storageSearchForm.getPageNum();
        Integer pageSize = storageSearchForm.getPageSize();
        StorageQueryCondition qc = storageSearchForm.getQueryCondition();

        IPage<Storage> iPage = storageDao.dynamicQuery(
                new Page<Storage>(pageNum, pageSize), qc);
        PagedResults<Storage> pagedResults = new PagedResults<>();
        pagedResults.setTotal(iPage.getTotal());

        pagedResults.setResults(iPage.getRecords());
        return pagedResults;
    }
//    bill模块开发人员提出要求开放修改意向库存的接口

    @PutMapping("/intention")
    public Integer updateIntentionStock(@RequestBody BatchIntentionDto batchIntentionDto){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Cookie[] cookies = request.getCookies();
        if (cookies!=null){
            for (int i = 0; i < cookies.length; i++) {
                System.out.println(cookies[i].getName()+"=== >>>> "+cookies[i].getValue());
            }
        }


        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println("当前远程调用的用户名为："+name);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println("当前远程调用的:principle"+principal);
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        System.out.println("当前远程调用的 roles"+authorities);
//        数据校验
        if (batchIntentionDto.getQuantity()<=0) {
            throw new CommodityException("批次数量不能小于0");
        }
       return storageDao.updateIntentionStock(batchIntentionDto);
    }
}
